﻿
namespace StockModel
{
    /// <summary>
    /// 表示股票日K一天的数据。
    /// </summary>
    public class StockDayKLine
    {
        public int id = 0;
        public DateTime date;
        public double preclose;
        public double high;
        public double low;
        public double close;

        public static StockDayKLine? parse(string line)
        {
            try
            {
                var ss = line.Split(',');
                return new StockDayKLine()
                {
                    id = int.Parse(ss[0]),
                    date = DateTime.Parse(ss[1]),
                    preclose = double.Parse(ss[2]),
                    high = double.Parse(ss[3]),
                    low = double.Parse(ss[4]),
                    close = double.Parse(ss[5])
                };
            }
            catch { }

            return null;
        }

        // ,date,preclose,high,low,close
        // 0,2000 - 01 - 04,11.5400,11.8800,11.5000,11.7600
        public static List<StockDayKLine> read_csv(string filename)
        {
            List<StockDayKLine> sinfo = new List<StockDayKLine>();

            var lines = File.ReadAllLines(filename);
            foreach (var line in lines)
            {
                var ss = line.Split(',');
                try
                {
                    StockDayKLine info = new StockDayKLine()
                    {
                        id = int.Parse(ss[0]),
                        date = DateTime.Parse(ss[1]),
                        preclose = double.Parse(ss[2]),
                        high = double.Parse(ss[3]),
                        low = double.Parse(ss[4]),
                        close = double.Parse(ss[5])
                    };
                    sinfo.Add(info);
                }
                catch { continue; }
            }

            return sinfo;
        }


        /// <summary>
        /// 获得中值，mid = (low + high) / 2;
        /// </summary>
        /// <returns></returns>
        public double GetMid() => (low + high) / 2;

        /// <summary>
        /// 格式化输出，01, 2024-11-25, p: 15.32, h:15.88, l: 14.32, c: 15.33
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            return $"{id}, {date:yyyy-MM-dd}, p: {preclose:00.00}, h: {high:00.00}, l: {low:00.00}, c: {close:00.00}";
        }
    }
}
